var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  data: {
    currentTab: 0,
    "value": "",   // 文本的内容
    "placeholder": "请输入文本",
    "maxlength": -1,  // 最大输入长度，设置为 -1 的时候不限制最大长度
    "focus": true,
    "auto-height": true,  // 是否自动增高，设置auto-height时，style.height不生效
    "adjust-position": true, // 键盘弹起时，是否自动上推页面
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Edit tab'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '编辑标签'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    var attendeeid = options.attendeeid
    that.setData({
      attendeeid: attendeeid
    });
    //调用pico标签列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var url = app.globalData.picoaddress + '/attendee/tags/list'
    var k = {}
    k["v"] = '1.0.0'
    k["client"] = 'ios'
    k["timestamp"] = timestamp
    k["lan"] = 'cn'
    k["exhibitionid"] = exhibitionid
    k["attendeeid"] = attendeeid
    k["u"] = u
    var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.hexMD5(sign)
    console.log(curb64)
    k["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: url,
      method: 'post',
      data: k,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        var curdata = mesg.data
        
        that.setData({
          tagsDetail: curdata
        });

      }, fail: res => {
        console.log(res, "error")
      }

    })
  },
  onPullDownRefresh: function(){
    wx.stopPullDownRefresh({
      complete: (res) => {},
    });
  },
  //底部导航跳转
  toAiIndex: function () {
    util.toAiIndex()
  },
  toContactsIng: function () {
    util.toContactsIng()
  },
  toProductRevision: function () {
    util.toProductRevision()
  },
  toMy: function () {
    util.toMy()
  },
  toBadge: function () {
    util.toBadge()
  },
  add: function () {
    //点击笔记跳转保存笔记页
    var that = this
    var attendeeid = that.data.attendeeid
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    //获取tagMesg
    var tagMesg = that.data.tagMesg
    //获取后置空tagMesg
    that.setData({
      tagMesg: ''
    });
    if(null!=tagMesg && tagMesg!=''){
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/tags/add'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["attendeeid"] = attendeeid
      k["tagname"] = tagMesg
      k["u"] = u
      var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'tagname' + tagMesg+ 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.zhMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          //添加后重新加载标签
          var timestamp = Date.parse(new Date());
          timestamp = timestamp / 1000;
          var url = app.globalData.picoaddress + '/attendee/tags/list'
          var t = {}
          t["v"] = '1.0.0'
          t["client"] = 'ios'
          t["timestamp"] = timestamp
          t["lan"] = 'cn'
          t["exhibitionid"] = exhibitionid
          t["attendeeid"] = attendeeid
          t["u"] = u
          var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
          console.log(sign)
          var curb64 = md5.zhMD5(sign)
          console.log(curb64)
          t["sign"] = curb64
          wx.request({
            header: {
              'content-type': 'application/x-www-form-urlencoded',
            },
            url: url,
            method: 'post',
            data: t,
            success(res) {
              console.log(res)
              var mesg = res.data
              console.log('mesg=' + mesg)
              var curdata = mesg.data

              that.setData({
                tagsDetail: curdata
              });

            }, fail: res => {
              console.log(res, "error")
            }

          })

        }, fail: res => {
          console.log(res, "error")
        }
      })
    }
  
  },
  inputChange: function (e) {
    var that = this;
    var tagMesg = e.detail.value
    that.setData({
      tagMesg: tagMesg
    });
    },
  deltag: function (e) {
    var that = this;
    var attendeeid = that.data.attendeeid
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    var tagid = e.currentTarget.dataset.tagid;
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var url = app.globalData.picoaddress + '/attendee/tags/customdel'
    var k = {}
    k["v"] = '1.0.0'
    k["client"] = 'ios'
    k["timestamp"] = timestamp
    k["lan"] = 'cn'
    k["exhibitionid"] = exhibitionid
    k["attendeeid"] = attendeeid
    k["tagid"] = tagid
    k["u"] = u
    var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'tagid' + tagid + 'timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.zhMD5(sign)
    console.log(curb64)
    k["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: url,
      method: 'post',
      data: k,
      success(res) {
        console.log(res)
        //删除后重新加载标签
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        var url = app.globalData.picoaddress + '/attendee/tags/list'
        var t = {}
        t["v"] = '1.0.0'
        t["client"] = 'ios'
        t["timestamp"] = timestamp
        t["lan"] = 'cn'
        t["exhibitionid"] = exhibitionid
        t["attendeeid"] = attendeeid
        t["u"] = u
        var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
        console.log(sign)
        var curb64 = md5.zhMD5(sign)
        console.log(curb64)
        t["sign"] = curb64
        wx.request({
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          url: url,
          method: 'post',
          data: t,
          success(res) {
            console.log(res)
            var mesg = res.data
            console.log('mesg=' + mesg)
            var curdata = mesg.data

            that.setData({
              tagsDetail: curdata
            });

          }, fail: res => {
            console.log(res, "error")
          }

        })
      }, fail: res => {
        console.log(res, "error")
      }
    })

  },
})